package cn.edu.seu.herald.informationplatform.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.seu.herald.informationplatform.dao.cookie.LeagueUserSessionCookieAccess;
import cn.edu.seu.herald.informationplatform.dao.cookie.SessionIdNotFound;
import cn.edu.seu.herald.informationplatform.dao.database.ActivityDao;
import cn.edu.seu.herald.informationplatform.filter.ActivityRequired;

public class ActivityDelete extends HttpServlet {

	private static final boolean SUCCESS = true;

	private static final boolean FAILURE = false;

	private static final String SUCCESS_MESSAGE = "操作成功";

	private static final String FAILURE_MESSAGE = "操作失败";

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response)
					throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		try {
			int activityId = Integer.valueOf(request.getParameter(
					ActivityRequired.PARAM_NAME_OF_ACTIVITY_ID));
			ActivityDao activityAccess = ActivityDao.getDao();
			activityAccess.deleteActivity(activityId);
			LeagueUserSessionCookieAccess luSessionCookieAccess =new LeagueUserSessionCookieAccess(request);
			response.sendRedirect("./activities.jsp?league="+luSessionCookieAccess.getLeagueId());
		} catch (SQLException e) {
			printResult(response, FAILURE);
			return;
		} catch (NumberFormatException e) {
			printResult(response, FAILURE);
			return;
		} catch (SessionIdNotFound e) {
			printResult(response, FAILURE);
			return;
		}
		printResult(response, SUCCESS);
	}
	
	private void printResult(HttpServletResponse response,
			boolean success) throws IOException {
		response.getWriter().println((success) ?
				SUCCESS_MESSAGE : FAILURE_MESSAGE);
	}

}
